package io.renren.modules.backstage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.backstage.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import io.renren.modules.backstage.dao.ChapterDao;
import io.renren.modules.backstage.entity.ChapterEntity;
import springfox.documentation.service.ApiListing;

import javax.annotation.Resource;


@Service("chapterService")
public class ChapterServiceImpl extends ServiceImpl<ChapterDao, ChapterEntity> implements ChapterService {
@Resource
ChapterDao chapterDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ChapterEntity> chapterEntityQueryWrapper = new QueryWrapper<>();
        if (params.get("key") != null && !params.get("key").equals("")) {
            chapterEntityQueryWrapper.like("chapter_name",params.get("key"));
        }
        IPage<ChapterEntity> page = this.page(
                new Query<ChapterEntity>().getPage(params),
                chapterEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage1(Map<String, Object> params) {
        Long pageNum = Long.parseLong((String) params.get("page"));
        Long pageSize = Long.parseLong((String) params.get("limit"));
        Page<ChapterEntity> page = new Page<ChapterEntity>(pageNum,pageSize);
        QueryWrapper wrapper = new QueryWrapper<>();
        if (params.get("key") != null && !params.get("key").equals("")) {
            wrapper.like("chapter_name",params.get("key"));
        }
        wrapper.eq("id",params.get("id"));
        Page page1 = chapterDao.selectPage(page, wrapper);
        return new PageUtils(page1);
    }

    @Override
    public void deleteById(Long id) {
        QueryWrapper<ChapterEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        chapterDao.delete(queryWrapper);
    }

    @Override
    public List<ChapterEntity> getAll(Long id) {
        QueryWrapper<ChapterEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
       return chapterDao.selectList(queryWrapper);
    }

}